package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.SysGoods;
import com.lovo.dto.SysGoodsDto;
import com.lovo.mapper.SysGoodsMapper;
import com.lovo.service.FShopQDService;
import com.lovo.service.ISysGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * ??̨??????Ʒ? 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class SysGoodsServiceImpl extends ServiceImpl<SysGoodsMapper, SysGoods> implements ISysGoodsService {

    @Autowired
    MQSService mqsService;


    @Autowired
    FShopQDService fShopQDService;

    @Override
    public IPage<SysGoods> findGoodsByItem(Integer PageNO, String goodsName, String goodsType) {
        QueryWrapper<SysGoods> qw = new QueryWrapper<>();
        if (goodsName != null & goodsName.length() != 0) {
            qw.like("goodsName", goodsName);
        }
        if (goodsType != null & goodsType.length() != 0) {
            qw.like("p_type", goodsType);
        }
        qw.eq("p_status", "已上架");

        return this.getBaseMapper().selectPage(new Page(PageNO, 5), qw);
    }

    @Override
    public void updateStatusByGoodsId(Integer goodsId) {
        SysGoods goods = this.getBaseMapper().selectById(goodsId);
        if (goods.getPStatus().equals("未上架")) {
            goods.setPStatus("已上架");
            this.getBaseMapper().updateById(goods);
//     上架商品 往前端发送 前端 存数据库
            SysGoods sysGoods = getBaseMapper().selectById(goodsId);
            SysGoodsDto sysGoodsDto = new SysGoodsDto();
            BeanUtils.copyProperties(sysGoods, sysGoodsDto);
            mqsService.sendGoods(sysGoodsDto);


        } else {
            goods.setPStatus("未上架");
            this.getBaseMapper().updateById(goods);
            //下架商品调用前端接口修改 商城前端商品状态改为已下架
            fShopQDService.downProduct(goodsId);
        }
    }

    @Override
    public IPage<SysGoods> findAllGoods(Integer PageNO, String goodsName, String goodsType) {

        QueryWrapper<SysGoods> qw = new QueryWrapper<>();
        if (goodsName != null & goodsName.length() != 0) {
            qw.like("goodsName", goodsName);
        }
        if (goodsType != null & goodsType.length() != 0) {
            qw.like("p_type", goodsType);
        }

        return this.getBaseMapper().selectPage(new Page(PageNO, 5), qw);
    }

    @Override
    public IPage<SysGoods> findGoodsByStatus(Integer PageNO) {
        QueryWrapper<SysGoods> qw = new QueryWrapper<>();
        qw.eq("p_status", "已上架");
        return this.getBaseMapper().selectPage(new Page(PageNO, 5), qw);
    }

    @Override
    public void sellGoods(Integer goodsId, Integer num) {
        QueryWrapper<SysGoods> qw = new QueryWrapper<>();
        qw.eq("goodsId", goodsId);
        SysGoods sysGoods = getBaseMapper().selectOne(qw);
        sysGoods.setStocknum(sysGoods.getStocknum() - num);
        getBaseMapper().updateById(sysGoods);
    }

    @Override
    public void sendProduct(SysGoods goods) {
        getBaseMapper().insert(goods);
    }


}
